package com.youmingxing.activity.selfInfo;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.adpter.selfInfo.AuditionRankAdapter;
import com.youmingxing.adpter.selfInfo.AuditionTopRankAdapter;
import com.youmingxing.bean.AuditionResultFromId;
import com.youmingxing.bean.selfinfo.AuditionRank;
import com.youmingxing.data.GlobalDataManager;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by JHS on 2015/9/19
 * Edited by ChenZhixi
 * 我的海选 结果确认中
 */
public class MyAuditionEndActivity extends BaseActivity {

    private List<AuditionRank> auditionRankList = new ArrayList<>();
    private AuditionRankAdapter adapter;
    private RefreshListView listView;
    private AuditionResultFromId resultFromId;
    private int id;
    private int mark = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_myauditions);
        setTopbarName(R.string.auditions_end_title);
        listView = (RefreshListView) findViewById(R.id.list_view);
        Bundle bundle = getIntent().getExtras();
        id = bundle.getInt("id");

        AllStarApi.showAudition(id, resultHandler);

    }

    BaseJsonHandler resultHandler =new BaseJsonHandler(this){
        @Override
        public void onSuccessWithData(JSONObject response) {
            JSONObject data = response.optJSONObject("data");
            resultFromId = new AuditionResultFromId(data);
            View headView = getLayoutInflater().inflate(R.layout.activity_myauditions_waitresul_top, null);
            boolean isMine = GlobalDataManager.userId == resultFromId.u_id;
            initTopView(headView, isMine);
            listView.addHeaderView(headView);
            adapter = new AuditionRankAdapter(MyAuditionEndActivity.this);
            listView.setAdapter(adapter);
            AllStarApi.getAuditionRank(resultFromId.movie_id, resultFromId.category_id, mark, rankHandler);
            listView.setOnLoadMoreListener(new RefreshListView.OnLoadMoreListener() {
                @Override
                public void onLoadMore() {
                    AllStarApi.getAuditionRank(resultFromId.movie_id, resultFromId.category_id, mark, rankHandler);
                }
            });

        }
    };
    BaseJsonHandler rankHandler = new BaseJsonHandler(this){
        @Override
        public void onSuccessWithData(JSONObject response) {
            listView.endLoadMore();
            JSONArray data = response.optJSONArray("data");
            if (data.length() <20) {
                listView.noMoreData();
            }
            for (int i = 0; i < data.length(); i++) {
                AuditionRank rank = new AuditionRank(data.optJSONObject(i));
                auditionRankList.add(rank);
            }
            if (mark == 1) {
                int topLength = auditionRankList.size() > 4 ? 4 : auditionRankList.size();
                List<AuditionRank> topRank = new ArrayList<>();
                for (int i = 0; i < topLength; i++) {
                    topRank.add(auditionRankList.get(0));
                    auditionRankList.remove(0);
                }

                AuditionTopRankAdapter topRankAdapter = new AuditionTopRankAdapter(MyAuditionEndActivity.this, topRank);
                gridView.setAdapter(topRankAdapter);
            }

            adapter.setAuditionRankList(auditionRankList);
            adapter.notifyDataSetChanged();
            mark++;
        }
    };

    private GridView gridView;

    private void initTopView(View view, boolean isMine) {
        TextView resultTickets = (TextView) view.findViewById(R.id.myauditionswait_result_tv3);
        TextView resultRanking = (TextView) view.findViewById(R.id.myauditionswait_result_tv4);
        TextView resultText1 = (TextView) view.findViewById(R.id.myauditionswait_result_tv1);
        TextView resultText2 = (TextView) view.findViewById(R.id.myauditionswait_result_tv2);
        gridView = (GridView) view.findViewById(R.id.myauditionswait_finalresult_gridview);

        resultText1.setText(resultFromId.motto);
        resultText2.setText(resultFromId.motto_tip);
        String ticketNum = getResources().getString(R.string.myauditionswait_result2) + resultFromId.votes;
        SpannableStringBuilder style = new SpannableStringBuilder(ticketNum);
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#E63E3E")), 5, ticketNum.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        resultTickets.setText(style);

        String rankString = getResources().getString(R.string.myauditionswait_result3) + resultFromId.rank;
        SpannableStringBuilder style2 = new SpannableStringBuilder(rankString);
        style2.setSpan(new ForegroundColorSpan(Color.parseColor("#E63E3E")), 5, rankString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        resultRanking.setText(style2);
    }


}